// 首页接口
const express = require("express");
const route = express.Router();
const pool = require("../utils/mysql");
// 引入验证
const Joi = require("joi");

// 获取首页数据大分类
route.get("/index/info", (req, res) => {
  pool.query("select * from pet_variety", (err, r) => {
    // 错误返回原因
    if (err) {
      res.send({ code: 500, msg: err });
    } else {
      // 成功返回
      res.send({ code: 200, msg: "ok", data: r });
    }
  });
});

// 首页模糊查询
route.get("/index/search", (req, res) => {
  let { p_name } = req.query;

  let schema = Joi.object({
    p_name: Joi.string().required(), // 必填
  });
  let { error } = schema.validate(req.query);
  if (error) {
    res.send({ code: 401, err: error.details });
    return; // 结束
  }

  pool.query(
    `select * from pet_pet where p_name like ?`,
    [`%${p_name}%`],
    (err, r) => {
      // 错误返回原因
      if (err) {
        res.send({ code: 500, msg: err });
      } else {
        // 成功返回
        res.send({ code: 200, msg: "ok", data: r });
      }
    }
  );
});

// 首页添加数据
route.post("/index/insert", (req, res) => {
  // post传递参数在body里面
  let { v_name } = req.body;

  let schema = Joi.object({
    v_name: Joi.string().required(), // 必填
  });
  let { error } = schema.validate(req.body);
  if (error) {
    res.send({ code: 401, err: error.details });
    return; // 结束
  }

  let sql = `INSERT INTO pet_variety (v_id,v_name) VALUES (NULL,?)`;
  pool.query(sql, [v_name], (err, r) => {
    if (err) {
      res.send({ code: 500, msg: err });
    } else {
      res.send({ code: 200, msg: "添加成功" });
    }
  });
});

// 首页删除数据
route.delete("/index/delete", (req, res) => {
  let { v_id } = req.query;

  let schema = Joi.object({
    v_id: Joi.string().required(), // 必填
  });
  let { error } = schema.validate(req.query);
  if (error) {
    res.send({ code: 401, err: error.details });
    return; // 结束
  }

  let sql = `DELETE FROM pet_variety WHERE v_id=?`;
  pool.query(sql, [v_id], (err, r) => {
    if (err) {
      res.send({ code: 500, msg: err });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});

// 通过大类获取所有小类宠物信息
route.get("/index/variety", (req, res) => {
  let { v_id } = req.query;

  let schema = Joi.object({
    v_id: Joi.string().required(), // 必填
  });
  let { error } = schema.validate(req.query);
  if (error) {
    res.send({ code: 401, err: error.details });
    return; // 结束
  }

  let sql = "SELECT * FROM pet_pet where v_id=?;";
  pool.query(sql, [v_id, v_id], (err, r) => {
    if (err) {
      res.send({ code: 500, msg: err });
    } else {
      res.send({ code: 200, msg: r });
    }
  });
});

// 获取动物详情
route.get("/index/detail", (req, res) => {
  let { v_id, p_id } = req.query;
  //   console.log(v_id, p_id);
  let schema = Joi.object({
    v_id: Joi.string().required(), // 必填
    p_id: Joi.string().required(), //必填
  });
  let { error } = schema.validate(req.query);
  if (error) {
    res.send({ code: 401, err: error.details });
    return; // 结束
  }
  let sql = "SELECT * FROM pet_pet where v_id=? and p_id=? ;";
  pool.query(sql, [v_id, p_id], (err, r) => {
    if (err) {
      res.send({ code: 500, msg: err });
    } else {
      res.send({ code: 200, msg: r });
    }
  });
});

module.exports = route;
